<template>
	<view class="container">
		<view class="left-bottom-sign"></view>
		<!-- <view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view> -->
		<view class="right-top-sign"></view>
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">{{title}}</view>
			<view class="welcome">
				欢迎回来！
			</view>
			<view class="input-content">
				<view class="input-item">
					<text class="tit">平台账号</text>
					<input type="text" v-model="formData.username" placeholder="请输入平台账号" data-key="name"
						@input="inputChange" />
				</view>
				<view class="input-item">
					<text class="tit">密码</text>
					<input type="mobile" v-model="formData.password" value="" placeholder="1-18位不含特殊字符的数字、字母组合"
						placeholder-class="input-empty" maxlength="20" password data-key="pass" @input="inputChange"
						@confirm="goLogin" />
				</view>
			</view>
			<button class="confirm-btn" @click="goLogin">确认</button>
			<button class="cancle-btn" @click="goBack">取消</button>
			<!-- <view class="forget-section">
				忘记密码?
			</view> -->
		</view>
		<view class="register-section">
			<image src="/static/img/user/icon_goback.png" class="img" mode="aspectFill" @click="goBack"></image>
			还没有账号?
			<text>请联系平台管理员</text>
			<!-- <text @click="toRegist">马上注册</text> -->
		</view>
	</view>
</template>

<script>
	import {
		Platform,
		Storage_Key,
		UserJobsType,
	} from "@/static/config.js"
	import {
		GetUserJobKeys,
	} from '@/common/util.js'
	import request from '@/common/request.js';
	export default {
		data() {
			return {
				title: Platform.name,
				loginBtnLoading: false,
				formData: { // PC账号密码
					username: "",
					password: "",
				},
				clientHight: '',
				footerHight: '',
				userData: { // 微信账号+PC账号ID
					userid: "",
					nickname: "",
					avatarurl: "",
					city: "",
					province: "",
					country: "",
					// gender: "",
					// language: "",
					openid: "",
					// sessionkey: "",
				},
			}
		},
		onReady() {
			// this.getClientHight();

		},
		onLoad() {},
		methods: {
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			async goLogin() {
				let _self = this;
				if (!_self.formData.username) {
					uni.showToast({
						title: '请输入账号',
						icon: 'none'
					});
					return 0;
				}
				if (!_self.formData.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return 0;
				}
				this.saveFormData();
			},
			async saveFormData() {
				uni.showLoading({
					title: '绑定...',
					mask: false
				});
				let _self = this;

				// v1.0通过osp/dologin 接口登录
				// uni.request({
				// 	url: Platform.api_osp_requestUrl + '/dologin',
				// 	data: _self.formData,
				// 	header: {
				// 		'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
				// 	},
				// 	method: 'POST',
				// 	success: (response) => {
				// 		console.log(response)
				// 		if (!!response.data.data) {
				// 			_self.doBindWechatAndWeb(response.data.data);
				// 		} else {
				// 			uni.hideLoading();
				// 			uni.showModal({
				// 				content: '平台账号登录失败',
				// 				showCancel: false
				// 			})
				// 		}
				// 	},
				// 	fail: (error) => {
				// 		uni.hideLoading();
				// 		uni.showModal({
				// 			content: '平台账号登录失败',
				// 			showCancel: false
				// 		})
				// 	},
				// });

				// v2.0 通过stp后台转发osp/dologin接口登录
				let paramstr = "username=" + _self.formData.username + "&password=" + _self.formData.password
				let commonpa = {
					"requestHeader": {
						"Content-Type": "application/x-www-form-urlencoded",
						"Charset": "UTF-8"
					},
					"requestMethod": "POST",
					"outputStr": paramstr,
					"requestUrl": Platform.api_osp_requestUrl + "/dologin"
				}
				request({
					url: Platform.api_requestUrl + '/request/httpsrequest',
					data: commonpa,
					method: 'POST',
				}).then(res => {
					if (!!res.data && res.code==200 && res.data.data) {
						_self.doBindWechatAndWeb(res.data.data);
					} else {
						uni.hideLoading();
						uni.showModal({
							content: res.message || res.data.message || '平台账号登录失败',
							showCancel: false
						})
					}
				}).catch(error => {
					console.log("error")
					uni.hideLoading();
					uni.showModal({
						content: '平台账号登录失败',
						showCancel: false
					})
				});
			},
			doBindWechatAndWeb(webUserInfo) {
				let _self = this;
				// 单独处理webuser的job
				let ukeys = GetUserJobKeys(webUserInfo.roles);
				let userkeys = ukeys.length > 0 ? ukeys : [UserJobsType[2].key]; //  微信授权 && 未绑定平台账号
				webUserInfo.job = userkeys;
				// 用户微信账号信息
				let userInfo = this.$storage.get(Storage_Key.user_info, true);
				// PC字段与微信字段不同
				_self.userData.nickname = userInfo.nickName;
				_self.userData.avatarurl = userInfo.avatarUrl;
				_self.userData.city = userInfo.city;
				_self.userData.province = userInfo.province;
				_self.userData.country = userInfo.country;
				// _self.userData.gender = userInfo.gender;
				// _self.userData.language = userInfo.language;
				_self.userData.openid = userInfo.openId;
				// _self.userData.sessionkey = userInfo.;
				_self.userData.userid = webUserInfo.user.userId;
				request({
					url: Platform.api_requestUrl + '/osp_basemain_wechatuserinfo/SaveOrUpdate',
					data: _self.userData,
					method: 'POST',
				}).then(response => {
					uni.hideLoading();
					if (!!response.data) {
						uni.showModal({
							title: '平台绑定成功',
							// content: '请妥善保存回执码：' + response.data.code,
							showCancel: false,
							confirmText: "确定",
							success: function(res) {
								if (res.confirm) {
									// 1、存储通知信息-同步
									_self.$storage.set(Storage_Key
										.web_user_info,
										webUserInfo,
										true
									);
									// _self.$storage.set(Storage_Key.user_info_typeindex, '2',false);
									// 2、发送事件
									uni.$emit("RefreshUser", {
										key: true,
										msg: webUserInfo
									})
									// 3、跳转
									_self.goBack();
								} else if (res.cancel) {}
							}
						});
					} else {
						uni.showModal({
							content: '绑定失败',
							showCancel: false
						})
					}
				}).catch(error => {
					uni.hideLoading();
					uni.showModal({
						content: '绑定失败',
						showCancel: false
					})
				});
			},
			goBack() {
				uni.switchTab({
					url: '/pages/user/user'
				})
			}
		}
	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		padding-top: 115px;
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.left-top-sign {
		font-size: 120upx;
		color: $page-color-base;
		position: relative;
		left: -16upx;
	}

	.right-top-sign {
		position: absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;

		&:before,
		&:after {
			display: block;
			content: "";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}

		&:before {
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}

		&:after {
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}

	.left-bottom-sign {
		position: absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}

	.welcome {
		position: relative;
		left: 50upx;
		top: -90upx;
		font-size: 46upx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0, 0, 0, .3);
	}

	.input-content {
		padding: 0 60upx;
	}

	.input-item {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		justify-content: center;
		padding: 0 30upx;
		background: $page-color-light;
		height: 120upx;
		border-radius: 4px;
		margin-bottom: 50upx;

		&:last-child {
			margin-bottom: 0;
		}

		.tit {
			height: 50upx;
			line-height: 56upx;
			font-size: $font-sm+2upx;
			color: $font-color-base;
		}

		input {
			height: 60upx;
			font-size: $font-base + 2upx;
			color: $font-color-dark;
			width: 100%;
		}
	}

	.confirm-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 70upx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;

		&:after {
			border-radius: 100px;
		}
	}

	.cancle-btn {
		width: 630upx;
		height: 76upx;
		line-height: 76upx;
		border-radius: 50px;
		margin-top: 10upx;
		color: #606266;
		font-size: $font-lg;

		&:after {
			border: none;
			background-color: none;
			border-radius: 100px;
		}
	}

	.forget-section {
		font-size: $font-sm+2upx;
		color: $font-color-spec;
		text-align: center;
		margin-top: 40upx;
	}

	.register-section {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm+2upx;
		color: $font-color-base;
		text-align: center;
		display: flex;
		justify-content: center;

		.img {
			width: 40upx;
			height: 40upx;
			margin-right: 20upx;
		}

		text {
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}
</style>